﻿#region File Description
//-----------------------------------------------------------------------------
// SpinningTriangleControl.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
using Keys = Microsoft.Xna.Framework.Input.Keys;
using MegavaniaX;
using MegavaniaX.World;
using MegavaniaXLevelEditor;

namespace WinFormsGraphicsDevice
{
    /// <summary>
    /// Example control inherits from GraphicsDeviceControl, which allows it to
    /// render using a GraphicsDevice. This control shows how to draw animating
    /// 3D graphics inside a WinForms application. It hooks the Application.Idle
    /// event, using this to invalidate the control, which will cause the animation
    /// to constantly redraw.
    /// </summary>
    public class LevelEditorMainControl : GraphicsDeviceControl
    {

        public World currentWorld;
        public MegavaniaXLevelEditorMain editor;
        private Texture2D gridTexture;
        private Texture2D hoveredTileTex;

        private SpriteBatch spriteBatch;

        private TextureRepo myrepo;
        private ContentBuilder contentBuilder;
        private ContentManager contentManager;

        private float currentMouseX;
        private float currentMouseY;

        private int currentTileX;
        private int currentTileY;

        private float Zoom;

        private KeyboardState oldKeyState;
        private MouseState oldMouseState;

        /// <summary>
        /// Initializes the control.
        /// </summary>
        protected override void Initialize()
        {
            // Hook the idle event to constantly redraw our LEVEL.
            Application.Idle += delegate { Invalidate(); };
            gridTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            gridTexture.SetData<UInt32>(new UInt32[] { 0xFF0000  });
            hoveredTileTex = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            hoveredTileTex.SetData<UInt32>(new UInt32[] { 0x00FF00 });
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Zoom = 2.0f;

            myrepo = new TextureRepo();
            Application.Idle += delegate { Invalidate(); };
            contentBuilder = new ContentBuilder();
            contentManager = new ContentManager(this.Services, contentBuilder.OutputDirectory);
            string be = myrepo.AddTilesToContent(contentBuilder, contentManager);
            if (!string.IsNullOrEmpty(be))
            {
                MessageBox.Show(be);
            }

            oldKeyState = Keyboard.GetState();
            oldMouseState = Mouse.GetState();

        }

        private void UpdateThings()
        {
            Mouse.WindowHandle = this.Handle;           
            var keyState = Keyboard.GetState();
            var mouseState = Mouse.GetState();

            

            oldKeyState = keyState;
            oldMouseState = mouseState;
            
        }



        protected void DrawGrid()
        {
            // draw highlighted tile
            spriteBatch.Draw(hoveredTileTex, new Rectangle(currentTileX * 16 * (int)Zoom, currentTileY * 16 * (int)Zoom, 16 * (int)Zoom, 16 * (int)Zoom), new Color(1.0f, 1.0f, 1.0f, 0.3f));

            // draw x
            for (int i = 0; i < Height; i += (int)(16 * Zoom))
            {
                spriteBatch.Draw(gridTexture, new Rectangle(0, i, Width, 1), Color.White);
            }

            // draw y
            for (int i = 0; i < Width; i += (int)(16 * Zoom))
            {
                spriteBatch.Draw(gridTexture, new Rectangle(i, 0, 1, Height), Color.White);
            }


        }

        /// <summary>
        /// Draws the control.
        /// </summary>
        protected override void Draw()
        {
            GraphicsDevice.Clear(Color.Black);
            // Set renderstates.
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;

            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
            DrawGrid();
            

            if (currentWorld != null)
            {

            }

            spriteBatch.End();
        
        }
    }
}
